<?php
session_start ();
require_once './AdminService.class.php';
if (isset ( $_POST ['form_submit'] ) and $_POST ['form_submit'] == "1") {
	$phoneNum = $_POST ['phoneNum'];
	// 实例化一个AdminServive方法,判断用户是否被注册
	$adminService = new AdminService ();
	$name = $adminService->chekcIsHaveAdimn ( $phoneNum );
	if (! $name) {
		header ( "Location: ../index.php?errno=1" ); // 该用户名已经存在
		exit ();
	}
	$password = $_POST ['password'];
	$nickname = $_POST ['nickname'];
	$sex = $_POST ['sex'];
	$birthday = $_POST ['birthday'];
	$isAdd = $adminService->addAdimn ( $phoneNum, $password, $nickname, $sex, $birthday );
	if (! $isAdd) {
		header ( "Location: ../index.php?errno=2" ); // 未知错误
		exit ();
	} else {
		$_SESSION ['userinfo'] = array (
				'userid' => $phoneNum,
				'username' => $nickname,
				'level' => 1 
		);
		header ( "Location: ../Space/SpaceIndex.php" ); // 注册成功
		exit ();
	}
} else if (isset ( $_POST ['form_submit'] ) and $_POST ['form_submit'] == "2") {
	$loginCAPTCHA = $_POST ['loginCAPTCHA'];
	// 判断验证码是否正确
	$checkCode = $_SESSION ['checkcode'];
	if ($loginCAPTCHA != $checkCode) {
		header ( "Location: ../index.php?errno=3" ); // 验证码错误
		exit ();
	}
	$loginUserId = $_POST ['phoneNum'];
	$loginPassText = $_POST ['password'];
	// 实例化一个AdminServive方法,判断用户是否被注册
	$adminService = new AdminService ();
	$name = $adminService->chekcAdimn ( $loginUserId, $loginPassText );
	if ($name == "1") {
		header ( "Location: ../index.php?errno=4" ); // 用户名或密码错误
		exit ();
	} else if ($name == "2") {
		header ( "Location: ./manage.php" ); // 管理员界面
		exit ();
	} else if ($name == "3") {
		header ( "Location: ../Space/SpaceIndex.php" ); // 会员登陆
		exit ();
	}
} else {
	// 清空session
	session_unset ();
	session_destroy ();
}
?>
